/*
 * Copyright (C), 2002-2016, 你的兔宝宝电子商务有限公司
 * FileName: CodeUtils.java
 * Author:   你的兔宝宝
 * Date:     2016年7月19日 下午1:11:47
 * Description: 代码工具//模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.baby.demo.utils;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baby.demo.utils.factory.StaticsClassFactoryBean;

import freemarker.template.Configuration;
import freemarker.template.SimpleHash;
import freemarker.template.Template;
import freemarker.template.TemplateHashModel;

/**
 * 代码工具<br> 
 * 〈功能详细描述〉
 *
 * @author 你的兔宝宝
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class Codes {
    
    private static final Logger logger = LoggerFactory.getLogger(Codes.class);
    
    public static Configuration cfg = new Configuration();
    static{
        try {
            TemplateHashModel sns = StaticsClassFactoryBean.factory("com.suning.demo.utils.Strings");
            TemplateHashModel snc = StaticsClassFactoryBean.factory("com.suning.demo.utils.Collections");
            Map<String,Object> variables = new HashMap<String,Object>();
            variables.put("sns", sns);
            variables.put("snc", snc);
            cfg.setAllSharedVariables(new SimpleHash(variables));
        } catch (Exception e) {
            logger.error("初始化配置异常",e);
            e.printStackTrace();
        }
    }
    
    @SuppressWarnings("unchecked")
    public static void generater4excel(String basePath,Map<String,Object> setting,String tplfile,String datafile,String[] pfields,String outputfile,String sheetName,int sheetAt){
        try {
            if( setting.containsKey("fields") ){
                List<String> fields = (List<String>) setting.get("fields");
                pfields = new String[fields.size()];
                pfields = fields.toArray(pfields);
            }
            basePath = Strings.get(setting, "basePath",basePath);
            tplfile = Strings.get(setting, "tplfile",tplfile);
            datafile = Strings.get(setting, "datafile",datafile);
            outputfile = Strings.get(setting, "outputfile",outputfile);
            sheetName = Strings.get(setting, "sheetName",sheetName);
            sheetAt = Strings.getInt(setting, "sheetAt", sheetAt);
            List<Map<String,Object>> data = null;
            if( Strings.isEmpty(sheetName) ){
                data = Excels.read(basePath + datafile, pfields, sheetAt);
            }else{
                data = Excels.read(basePath + datafile, pfields, sheetName);
            }
            Map<String,Object> model = new HashMap<String,Object>();
            model.put("data", data);
            model.putAll(setting);
            Template tpl = new Template("code-generater-tpl", new FileReader(new File(basePath + tplfile)), cfg);
            tpl.process(model, new FileWriter(new File( basePath + outputfile)));
        } catch (Exception e) {
            logger.error("generater code error",e);
            e.printStackTrace();
        }
    }
    
    public static void generater4excel(String basePath,String settingfile, String tplfile, String datafile){
        List<Map<String,Object>> settings = Jsons.jsonFile2list( Strings.show(basePath) + settingfile);
        for(Map<String,Object> setting : settings){
            boolean skip = Strings.getBoolean(setting,"skip", false);
            if( skip ) continue;
            generater4excel(basePath,setting, tplfile, datafile);
        }
    }
    
    private static void generater4excel(String basePath, Map<String, Object> setting, String tplfile, String datafile) {
        generater4excel(basePath, setting, tplfile, datafile, null, null, null, -1);
    }
    
    public static void generater4excel(String basePath,String settingfile,String tplfile,String datafile,String[] pfields,String outputfile,String sheetName,int sheetAt){
        Map<String,Object> setting = Jsons.jsonFile2map( Strings.show(basePath) + settingfile);
        generater4excel(basePath, setting, tplfile, datafile, pfields, outputfile, sheetName, sheetAt);
    }
    
    public static void generater4excel(String basePath, String settingfile, String tplfile, String datafile,String[] fields, String outputfile,String sheetName) {
        generater4excel(basePath, settingfile, tplfile, datafile, null, outputfile, sheetName, -1);
    }
    
    public static void main(String[] args) {
        String basePath = "F:\\doc\\project\\自营PC装修市场\\doc\\",tplfile = "sqlMap.ftl",datafile="装修表.xlsx",settingfile = "sqlMap_setting.json";
        generater4excel(basePath,settingfile, tplfile, datafile);
    }
    
}
